package com.lsh.radardemo;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;

public class Axis {
    private final FloatBuffer vertexBuffer;
    private final FloatBuffer colorBuffer;
    private final int mProgram;

    // 每个顶点有3个坐标
    static final int COORDS_PER_VERTEX = 3;

    private final String vertexShaderCode =
            "uniform mat4 uMVPMatrix;" +
                    "attribute vec4 vPosition;" +
                    "attribute vec4 aColor;" +
                    "varying vec4 vColor;" +
                    "void main() {" +
                    "  gl_Position = uMVPMatrix * vPosition;" +
                    "  vColor = aColor;" +
                    "}";

    private final String fragmentShaderCode =
            "precision mediump float;" +
                    "varying vec4 vColor;" +
                    "void main() {" +
                    "  gl_FragColor = vColor;" +
                    "}";

    public Axis(float length) {
        // 坐标轴顶点坐标 (X, Y, Z轴各2个点)
        float[] axisCoords = {
                // X轴 (红色) - 从 -length 到 +length
                -length, 0.0f, 0.0f,
                length, 0.0f, 0.0f,
                // Y轴 (绿色)
                0.0f, -length, 0.0f,
                0.0f,  length, 0.0f,
                // Z轴 (蓝色)
                0.0f, 0.0f, -length,
                0.0f, 0.0f,  length
        };

        // 坐标轴颜色 (R,G,B,A)
        float[] axisColors = {
                // X轴 - 红色
                1.0f, 0.0f, 0.0f, 1.0f,
                1.0f, 0.0f, 0.0f, 1.0f,
                // Y轴 - 绿色
                0.0f, 1.0f, 0.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,
                // Z轴 - 蓝色
                0.0f, 0.0f, 1.0f, 1.0f,
                0.0f, 0.0f, 1.0f, 1.0f
        };

        // 初始化顶点字节缓冲区
        ByteBuffer bb = ByteBuffer.allocateDirect(axisCoords.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(axisCoords);
        vertexBuffer.position(0);

        // 初始化颜色字节缓冲区
        ByteBuffer cb = ByteBuffer.allocateDirect(axisColors.length * 4);
        cb.order(ByteOrder.nativeOrder());
        colorBuffer = cb.asFloatBuffer();
        colorBuffer.put(axisColors);
        colorBuffer.position(0);

        // 准备着色器程序
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glLinkProgram(mProgram);
    }

    public void draw(float[] mvpMatrix) {
        // 添加程序到OpenGL ES环境
        GLES20.glUseProgram(mProgram);

        // 获取顶点着色器的vPosition成员句柄
        int mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        // 启用顶点属性
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // 准备坐标轴坐标数据
        GLES20.glVertexAttribPointer(
                mPositionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false,
                0, vertexBuffer);

        // 获取颜色属性句柄
        int mColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");
        GLES20.glEnableVertexAttribArray(mColorHandle);
        GLES20.glVertexAttribPointer(
                mColorHandle, 4,
                GLES20.GL_FLOAT, false,
                0, colorBuffer);

        // 获取变换矩阵句柄
        int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

        // 应用投影和视图变换
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

        // 绘制坐标轴 (GL_LINES模式)
        GLES20.glLineWidth(3.0f); // 设置线宽
        GLES20.glDrawArrays(GLES20.GL_LINES, 0, 6);

        // 禁用顶点数组
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mColorHandle);
    }

    public static int loadShader(int type, String shaderCode) {
        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }
}